package com.tutu.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Program: Spring_New
 * @Description:
 * @Author: JK.TuTu
 * @Create: 2022-09-15 19:45
 **/
public class ProxyInvocationHandler implements InvocationHandler {
    private Object target;
    public void setTarget(Object target) {
        this.target = target;
    }
    /**
     * 生成代理类
     *
     * @return 返回需要代理的对象
     */
    public Object getProxy() {
        return Proxy.newProxyInstance(this.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }

    /**
     * @param proxy  代理类
     * @param method 代理类的调用处理程序的方法对象
     * @param args
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before(method.getName());
        Object result = method.invoke(target, args);
        after(method.getName());
        return result;
    }

    public void before(String methodName) {
        System.out.println("开始执行" + methodName + "方法");
    }

    public void after(String methodName) {
        System.out.println(methodName + "方法执行完成");
    }
}

